package com.second.app.excutor;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author GYZ
 * @Date 2024/1/24
 * <pre>
 *   线程池中添加的数量>corePoolSize && <=MaximumPoolSize
 *   从运行结果来看 8个线程成功运行，同一时间最多只能有7个线程在运行，线程数量大于corePoolSize的时候，会将其余的任务
 *   放入到队列中（如果使用LinkedBlockingDeque类，则maximumPoolSize参数作用将忽略）
 * </pre>
 */
public class ThreadExecutorPoolRun {
    public static void main(String[] args) throws InterruptedException {

        Runnable runnable = () -> {
            try {
                System.out.println(Thread.currentThread().getName() + " run!" + System.currentTimeMillis());
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(7, 8,
                5, TimeUnit.SECONDS, new LinkedBlockingDeque<>());

        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);

        Thread.sleep(300);
        //可以理解为：车中可载人的标准人数
        System.out.println("A:"+threadPoolExecutor.getCorePoolSize());
        //可以理解为：车中正在载的人数
        System.out.println("A:"+threadPoolExecutor.getPoolSize());
        //扩展车中正在载的人数
        System.out.println("A:"+threadPoolExecutor.getQueue().size());

        Thread.sleep(1000);
        System.out.println("B:"+threadPoolExecutor.getCorePoolSize());
        System.out.println("B:"+threadPoolExecutor.getPoolSize());
        System.out.println("B:"+threadPoolExecutor.getQueue().size());
    }
}
